package com.zyh;

import com.zyh.util.ESUtils;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class test {

    @Test
    public void testInsert(){
        TransportClient client = ESUtils.getClient();
        //准备数据
        Map map=new HashMap();
        map.put("name","11");
        map.put("price",22);
        //执行插入
        IndexResponse indexResponse = client.prepareIndex("dang", "book", "10")
                .setSource(map).get();
        System.out.println(indexResponse);
        client.close();
    }
    @Test
    public void testDelete(){
        TransportClient client = ESUtils.getClient();
        //准备数据
        DeleteResponse deleteResponse = client.prepareDelete("dang", "book", "10").get();
        System.out.println(deleteResponse);
        client.close();
    }
    @Test
    public void testUpdate(){
        TransportClient client = ESUtils.getClient();
        //准备数据
        Map map=new HashMap();
        map.put("name","111");
        map.put("price",222);
        //执行插入
        UpdateResponse updateResponse = client.prepareUpdate("dang", "book", "10")
                .setDoc(map).get();
        System.out.println(updateResponse);
        client.close();
    }
    @Test
    public void testGet(){
        //准备连接
        TransportClient client = ESUtils.getClient();
        //执行查询
        SearchResponse searchResponse = client.prepareSearch("ems").setTypes("emp")
                .setQuery(QueryBuilders.multiMatchQuery("张黑java", "name", "content"))
                .setFrom(2).setSize(3)
                .addSort("age", SortOrder.ASC)
                .get();
        System.out.println(searchResponse);
        client.close();
    }
    @Test
    public void testGetHighlight(){
        //准备连接
        TransportClient client = ESUtils.getClient();
        //执行查询
        SearchResponse searchResponse = client.prepareSearch("ems").setTypes("emp")
                .setQuery(QueryBuilders.multiMatchQuery("java", "name", "content"))
                //设置高亮查询
                .highlighter(new HighlightBuilder().field("content").preTags("<span style='color:red'>").postTags("</span>"))
                .get();
        System.out.println(searchResponse);
        SearchHits hits = searchResponse.getHits();
//        System.out.println("查询出数据条数："+hits.totalHits);
        for (SearchHit hit:hits){
            //获取原始数据
//            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
//            System.out.println(sourceAsMap);
//            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
//            //获取高亮显示结果
//            HighlightField content = highlightFields.get("content");
//            if(content!=null){
//                Text fragment = content.fragments()[0];
//                System.out.println(fragment);
//            }
            System.out.println(hit);
        }

        client.close();
    }
}
